package middleware

import (
	"time"

	"github.com/cihub/seelog"
	"github.com/gin-gonic/gin"
)

// LoggerToSeelog 输出日志至seelog
func LoggerToSeelog() gin.HandlerFunc {
	return func(param *gin.Context) {
		// 开始时间
		startTime := time.Now()
		// 处理请求
		param.Next()
		// 结束时间
		endTime := time.Now()
		// 执行时间
		latencyTime := endTime.Sub(startTime)
		// 请求方式
		reqMethod := param.Request.Method
		// 请求路由
		reqURI := param.Request.RequestURI
		// 状态码
		statusCode := param.Writer.Status()
		// 请求IP
		clientIP := param.ClientIP()
		// 日志格式
		seelog.Infof("| %3d | %13v | %15s | %s | %s | %s |",
			statusCode,
			latencyTime,
			clientIP,
			reqMethod,
			reqURI,
			param.Request.UserAgent(),
		)
	}
}
